﻿//        var SysAgent = {};
//        var ua = navigator.userAgent.toLowerCase();
//        if (window.ActiveXObject)
//            SysAgent.ie = ua.match(/msie ([\d.]+)/)[1]
//        else if (document.getBoxObjectFor)
//            SysAgent.firefox = ua.match(/firefox\/([\d.]+)/)[1]
//        else if (window.MessageEvent && !document.getBoxObjectFor)
//            SysAgent.chrome = ua.match(/chrome\/([\d.]+)/)[1]
//        else if (window.opera)
//            SysAgent.opera = ua.match(/opera\/([\d.]+)/)[1]
//        else if (window.openDatabase)
//            SysAgent.safari = ua.match(/version\/([\d.]+)/)[1];
//            
//         //获取元素的上边沿的绝对位置
//       function getTop(e) {
//            var offset = e.offsetTop;
//            if (e.offsetParent != null) {
//                if (e.offsetParent.style.position.toLowerCase() == "absolute") {
//                    if (SysAgent.firefox)
//                        offset += parseInt(e.offsetParent.style.top);
//                    return offset;
//                }
//                offset += this.getTop(e.offsetParent);
//            }
//            return offset;
//        }
//        //获取元素的左边沿的绝对位置
//        function getLeft(e) {
//            var offset = e.offsetLeft;
//            if (e.offsetParent != null) {
//                if (e.offsetParent.style.position.toLowerCase() == "absolute") {
//                    if (SysAgent.firefox)
//                        offset += parseInt(e.offsetParent.style.left);
//                    return offset;
//                }
//                offset += this.getLeft(e.offsetParent);
//            }
//            return offset;
//        }
//        //隐藏body,出现半透明
//        function maskBody(bodyalpha, maskid) {
//            var maskDiv = document.getElementById(maskid);
//            //maskDiv.className = 'maskStyle';
//            maskDiv.style.visibility = "visible";
//            maskDiv.style.width = document.body.scrollWidth + "px";
//            maskDiv.style.height = document.body.scrollHeight + "px";
//            if (SysAgent.ie) {
//                //ie
//                maskDiv.style.filter = "alpha(opacity = " + bodyalpha.toString() + ")";
//            }
//            if (SysAgent.firefox) {
//                //ff
//                maskDiv.style.MozOpacity = bodyalpha / 100;
//            } if (SysAgent.chrome) {
//                //chrome
//                maskDiv.style.opacity = bodyalpha / 100;
//            }
//        }
//        //显示
//        function remaskBody(maskid) {
//            var maskDiv = document.getElementById(maskid);
//            maskDiv.style.visibility = "hidden";
//            maskDiv.style.width = "0px";
//            maskDiv.style.height = "0px";
//        }

//滚动图片效果
var detailImgsTimeId = null;
var timeidInterval = 10;
var genWidth = 150;
var genHeight = 120;
var maxWidth = 300;
var rate = 1.15;
var lastLargeImgEle = null;
function DetailImgsMove() {
    var detailImgsTable = document.getElementById("DetailImgsTable");
    var detailImgsTd1 = document.getElementById("DetailImgsTd1");
    var detailImgsTd2 = document.getElementById("DetailImgsTd2");
    var detailImgsDiv = document.getElementById("DetailImgsDiv");
    if (detailImgsTd2.offsetWidth - detailImgsDiv.scrollLeft <= 0)
        detailImgsDiv.scrollLeft -= detailImgsTd1.offsetWidth
    else
        detailImgsDiv.scrollLeft++;
}

(function initDetailImgs() {
    detailImgsTimeId = setInterval(DetailImgsMove, timeidInterval);
    var detailImgsTd1 = document.getElementById("DetailImgsTd1");
    var detailImgsTd2 = document.getElementById("DetailImgsTd2");
    var detailImgsDiv = document.getElementById("DetailImgsDiv");
    if (detailImgsTd1.getElementsByTagName("table")[0] == null)
        return;

    var inDivEle = detailImgsTd1.getElementsByTagName("table")[0].getElementsByTagName("div")[0];
    var totalCount = detailImgsTd1.getElementsByTagName("table")[0].getElementsByTagName("td").length;

    detailImgsDiv.style.width = parseInt((parseInt(document.documentElement.scrollWidth)) * 0.70) + "px";
    var width = parseInt((parseInt(document.documentElement.scrollWidth)) * 0.70);
    if (detailImgsDiv.offsetWidth < (2 * totalCount * inDivEle.offsetWidth)) {
        detailImgsTd2.innerHTML = detailImgsTd1.innerHTML;
        if (detailImgsDiv.offsetWidth > (totalCount * inDivEle.offsetWidth)) {
            var detailImgsTd3 = document.getElementById("DetailImgsTd3"); ;
            detailImgsTd3.innerHTML = detailImgsTd1.innerHTML;
        }

        detailImgsDiv.onmouseover = function() { clearInterval(detailImgsTimeId); }
        detailImgsDiv.onmouseout = function() { detailImgsTimeId = setInterval(DetailImgsMove, timeidInterval); }

    }
    var allAnchorList = detailImgsDiv.getElementsByTagName("a");
    for (var i = 0; i < allAnchorList.length; i++) {
        var titleTip = "点击查看放大图片";
        var orginImgEle = allAnchorList[i].getElementsByTagName("img")[0];
        if (orginImgEle.title == titleTip) {
            allAnchorList[i].onclick = function() {
                //                            var largeImgDiv = document.getElementById("LargeImgDiv");
                //                            var largeImgEle = largeImgDiv.getElementsByTagName("img")[0];
                //                            largeImgEle.src = this.getElementsByTagName("img")[0].src;
                //                            largeImgDiv.style.display = "block";
                //                            largeImgDiv.style.top = getTop(this) + "px";
                //                            largeImgDiv.style.left = getLeft(this) + "px";
                //                            maskBody(50, "maskDiv");


                var curImgEle = this.getElementsByTagName("img")[0];
                if (curImgEle == lastLargeImgEle) {
                    var curImgTimeId = setInterval(function() {
                        var _otherWidth = lastLargeImgEle.offsetWidth;
                        var _otherHeight = lastLargeImgEle.offsetHeight;
                        _otherWidth = parseInt(_otherWidth / rate);
                        _otherHeight = parseInt(_otherHeight / rate);
                        if (_otherWidth <= genWidth || _otherHeight <= genHeight) {
                            lastLargeImgEle.style.width = genWidth + "px";
                            lastLargeImgEle.style.height = genHeight + "px";
                            clearInterval(curImgTimeId);
                            detailImgsTimeId = setInterval(DetailImgsMove, timeidInterval);
                            lastLargeImgEle = null;
                            detailImgsDiv.onmouseout = function() { detailImgsTimeId = setInterval(DetailImgsMove, timeidInterval); }
                        }
                        else {
                            lastLargeImgEle.style.width = _otherWidth + "px";
                            lastLargeImgEle.style.height = _otherHeight + "px";
                        }
                    }, 10);
                    return;
                }

                //if (detailImgsTimeId != null) 
                {
                    clearInterval(detailImgsTimeId);
                    detailImgsDiv.onmouseout = null;
                }
                var largeImgTimeid = setInterval(function() {
                    var curWidth = curImgEle.offsetWidth;
                    var curHeight = curImgEle.offsetHeight;
                    curWidth = parseInt(curWidth * rate);
                    curHeight = parseInt(curHeight * rate);
                    if (curWidth <= maxWidth) {
                        curImgEle.style.width = curWidth + "px";
                        curImgEle.style.height = curHeight + "px";
                    }
                    else {
                        clearInterval(largeImgTimeid);
                        lastLargeImgEle = curImgEle;
                    }
                }, 10);

                for (var j = 0; j < allAnchorList.length; j++) {
                    var imgEle = allAnchorList[j].getElementsByTagName("img")[0];
                    if (imgEle.offsetWidth > genWidth && imgEle != curImgEle) {
                        var otherImgTimeId = setInterval(function() {
                            var _otherWidth = lastLargeImgEle.offsetWidth;
                            var _otherHeight = lastLargeImgEle.offsetHeight;
                            _otherWidth = parseInt(_otherWidth / rate);
                            _otherHeight = parseInt(_otherHeight / rate);
                            if (_otherWidth <= genWidth || _otherHeight <= genHeight) {
                                lastLargeImgEle.style.width = genWidth + "px";
                                lastLargeImgEle.style.height = genHeight + "px";
                                clearInterval(otherImgTimeId);
                            }
                            else {
                                lastLargeImgEle.style.width = _otherWidth + "px";
                                lastLargeImgEle.style.height = _otherHeight + "px";
                            }
                        }, 10);
                    }
                }


            }
        }
    }

})();